<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:sku_arr_info="{row}">
      <list :cols="sku_arr_config" :data="row.value"></list>
      <el-button v-if="can_sku" size="small" @click="open_iframe('sku_edit')">规格配置</el-button>
    </template>
    <template v-slot:additional_config="{row}">
      <p v-for="v in row.value" style="line-height: 1.6;padding: 02px;">
        <span v-if="v.required==1">(必选)</span>{{v.multiple==1?'多':'单'}}选项：{{v.title}}[<span v-for="v2 in v.list" class="button on" style="margin: 0 2px;">{{v2.title}}</span>]
      </p>
      <el-button v-if="can_additional" size="small" @click="open_iframe('additional_edit')">定制配置</el-button>
    </template>
  </forms>
</template>

<script>
export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model = {name:this.$get('model_name'), alias:this.$get('model_alias','')};
    let controller = this.camel_str(model.name);

    return {
      id: id,
      btns: btns,
      config: {},
      model: model,
      can_sku: in_right_admin(controller+'/sku_edit'),
      can_additional: in_right_admin(controller+'/additional_edit'),
      sku_arr_config: [
        {name:'title', label:'规格标题'},
        {name:'price', label:'价格', width: '120px'},
        {name:'price_vip', label:'会员价', width: '120px'},
        {name:'stock', label:'库存', width: '120px'},
        {name:'default', label:'默认', type:'radio_switch',config:'',width: '120px'},
      ],
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model.name;
      data.model_alias = this.model.alias;
      return s94Mjs.param_encode(data);
    },
    onsubmit(formData){
      let url = this.id ? `/admin/${this.model.name}/edit?id=`+this.id : `/admin/${this.model.name}/add`;
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('index?'+this.param(), '$refs.list_url.list');
          iframe.hide('edit?'+this.param());
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index?'+this.param());
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    open_iframe(url){
      url = url+(~url.indexOf('?')?'&':'?')+this.param({id:this.id});
      this.iframe(url);
    },
    get_info(){
      if (!this.id) return;
      ajax({url:'/admin/'+this.model.name+'/info?id='+this.id}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    function is_inline(v){
      return false;
      let map = ['radio_switch','select_one','date','date_between','datetime','datetime_between','duration']
      let is_number = v.type=='text_one' && v.config=='number';
      return (~map.indexOf(v.type) || is_number);
    }

    let is_info = this.$get('is_info');
    if (this.id) {
      ajax({url:'/admin/'+app.model.name+'/info?get_field_config=1&id='+this.id, cache:true}, (res)=>{
        app.model = res.field_config.model;
        // document.title = '后台-'+app.model.alias+'-添加';
        let form_config = {
          encode_id: {label:app.model.alias+'编号',name:'encode_id'},
        };
        if (is_info) app.btns = [];
        s94Mjs.each(res.field_config.cols, function(v){
          v.name = v.name.replace('.','_');
          form_config[v.name] = {
            label: v.label,
            name: v.name,
            tips: v.tips,
            type: v.type,
            is_info: v.can_edit!=1 || is_info,
            config: v.config,
            value:v.value,
          };
          form_config[v.name].inline = is_inline(form_config[v.name]);
          if (v.name=='price' && app.model.link_order==1){
            if(app.model.need_sku==1) form_config['sku_arr'] = {
              label: '规格列表',
              name: 'sku_arr',
              tips: '',
              type: 'sku_arr_info',
              config: '',
              value: [],
            };
            if(app.model.need_additional==1) form_config['additional_config'] = {
              label: '定制配置',
              name: 'additional_config',
              tips: '',
              type: 'additional_config',
              config: '',
              value: [],
            };
          }
        })
        app.config = form_config;
        setTimeout(function(){ app.$refs['forms'].init(res.data); });
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit?'+this.param());
        }else {
          this.$router.push('index?'+this.param());
        }
      })
    }else{
      ajax({url:'/admin/'+app.model.name+'/field_config?action=add&table=0', cache:true}, function(res){
        app.model = res.data.model;
        // document.title = '后台-'+app.model.alias+'-添加';
        let form_config = {};
        s94Mjs.each(res.data.cols, function(v){
          v.name = v.name.replace('.','_');
          form_config[v.name] = {
            label: v.label,
            name: v.name,
            tips: v.tips,
            type: v.type,
            config: v.config,
            value:v.value,
          };
          form_config[v.name].inline = is_inline(form_config[v.name]);
        })
        app.config = form_config;
        setTimeout(function(){app.$refs['forms'].set_init_value()})
        return false;
      })
    }

    //其他初始化操作
  }
}
</script>
